package com.bbg.amyam.web.util.view;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.text.WordUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.bbg.amyam.web.util.menu.MenuItem;

public class WebFlowServicerGenerator extends AbstractGenerator {
	private static final Log log = LogFactory.getLog(ControllerGenerator.class);
	
		
	public WebFlowServicerGenerator(MenuItem item, boolean overwrite) {
		super(item, overwrite);
	}

	public void execute() throws IOException {
		String targetParentDir = super.getWebflowServicerDirPath();
		String templateDir = super.getWebflowServiceTemplateFile();

		String packageDirName=this.menuItem.getTarget();
		packageDirName = StringUtils.replace(packageDirName,"_"," ");
		packageDirName = StringUtils.replace(packageDirName,"/",File.separator+" ");
		packageDirName = WordUtils.capitalize(packageDirName);
		packageDirName = StringUtils.deleteWhitespace(packageDirName);
		packageDirName = StringUtils.replace(packageDirName,File.separator,File.separator+" ");
		packageDirName = WordUtils.uncapitalize(packageDirName);
		packageDirName = StringUtils.deleteWhitespace(packageDirName);
		
		String packageName=StringUtils.replace(packageDirName,File.separator,".");
		
		String serviceClassName=this.menuItem.getId();
		serviceClassName=StringUtils.replace(serviceClassName,"_"," ");
		serviceClassName = WordUtils.capitalize(serviceClassName);
		serviceClassName = StringUtils.deleteWhitespace(serviceClassName);
		
		String serviceName = WordUtils.uncapitalize(serviceClassName);
		
		

		
		this.replacedValues
				.put("__packageName__", packageName);
		this.replacedValues.put("__serviceClassName__",serviceClassName);
		
		this.replacedValues.put("__serviceName__", serviceName);
		
		File srcDir=new File(templateDir);
		
		Collection<File> files = FileUtils.listFiles(srcDir, new String[] {"java"} , false);
		
		for(File file:files){
			String fileName=file.getName();
			String targetFileName=targetParentDir+packageDirName+File.separator+serviceClassName+fileName;
			log.debug("Copying  " + file.getPath()+File.separator+fileName + " to "
					+ targetFileName);
			File targetFile=new File(targetFileName);
			if(targetFile.exists()){
				if(this.overwrite){
					targetFile.delete();
				}else{
					log.debug("Skip copying " + file.getPath()+File.separator+fileName + " to "
							+ targetFileName);
					continue;
				}
			}
			copyFile(file, targetFile);
			log.debug("Copied  " + file.getPath()+File.separator+fileName + " to "
					+ targetFileName);
		}
		
	}

	private void copyFile(File src, File target) throws IOException {
		
		List<String> lines = FileUtils.readLines(src);
		ArrayList<String> newLines = new ArrayList<String>();
		boolean rmv=false;
		for (String line : lines) {
			String newLine = processLine(line);
			newLines.add(newLine);
		}

		FileUtils.writeLines(target, newLines);
	}
}
